最近工作要開發Sharepoint的客製功能,需要應用到 Taxonomy 欄位並用字詞組當選項,
然而在當建立好之後,新增資料時會出現"正在更新的 SPListItem 未擷取到所有分類欄位。"
研究一番之後了解知道怎麼正確建立欄位,並寫下來分享。
環境簡介:SharePoint 2016、Visual Studio 2017
以系統管理員開啟 Visual Studio -> 新增 -> 專案 -> 選擇 "SharePoint 2016 空專案" -> 輸入專案名稱後按確定。 ※因為要直接連線站台,要使用系統管理員開啟。
輸入站台網址,選擇佈署為陣列方案,按下完成。
由於專案名稱不是我要的,所以先刪掉專案。
在解決方案按下右鍵 -> 加入 -> 新增專案 -> 選擇空專案,名稱輸入 "mysite.column",按下確定 。
在專案上按下右鍵 -> 加入 -> 新增項目,選擇 "網站資料行",建立兩個欄位 "姓名(myName)"、"居住縣市(myLocation)",居住縣市是要拿來做Taxonomy的欄位。
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  <Field
       ID="{d1a3001d-8161-4a02-b4c0-45d069deccf3}"
       Name="myName"
       DisplayName="姓名"
       Type="Text"
       Required="FALSE"
       Group="Custom Site Columns">
  </Field>
</Elements>
ShowField="Term1028" 這段代碼,代表此欄位為zh-tw的地區設定。<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field
     ID="{47BF9D02-EFE2-4791-9C6D-7184629C06DC}"
     Name="myLocation_0"
     DisplayName="居住縣市_0"
     SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
     Type="Note"
     Group="Custom Site Columns"
     ShowInViewForms="FALSE"
     Hidden="TRUE">
  </Field>
  <Field
       ID="{4316be76-f19b-4af5-8446-1939f4e7747d}"
       Name="myLocation"
       DisplayName="居住縣市"
       Type="TaxonomyFieldType"
       ShowField="Term1028"
       Required="FALSE"
       Group="Custom Site Columns">
  </Field>
</Elements>
到網站的 網站設定 -> 網站管理 -> 新增如圖片的字詞組。   台灣行政區.csv
回到專案,在專案的 "參考" 加入 "Microsoft.Sharepoint.Taxonomy" 組件。
開啟Feature資料夾,在Feature1按下右鍵 -> 加入事件接收器,在EventReciver.cs 內引最上方引用using Microsoft.SharePoint.Taxonomy; 並在class內加上下面這段程式碼。
※ fieldIdBU、noteFieldIdBU 及 MapManagedMetadataField 的參數請依照自己的狀況修改
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = (properties.Feature.Parent as SPWeb).Site;    //依照專案部屬類型要隨著修改,此行是將專案部屬在web上
            // Map Managed Metadata site columns
            Guid fieldIdBU = new Guid("{4316be76-f19b-4af5-8446-1939f4e7747d}");    //Taxonomy欄位的GUID
            Guid noteFieldIdBU = new Guid("{47BF9D02-EFE2-4791-9C6D-7184629C06DC}");    //Note欄位的GUID
            
            //參數為 (Taxonomy欄位, Note欄位, 網站, 服務應用名稱, 字詞組群組, 字詞組名稱)
            MapManagedMetadataField(fieldIdBU, noteFieldIdBU, site, "Managed Metadata Service", "Taiwan", "台灣行政區");
        }
        private void MapManagedMetadataField(Guid fieldId, Guid noteFieldId, SPSite site, string sTermStore, string sTermGroup, string sTermSet)
        {
            if (site.RootWeb.Fields.Contains(fieldId))
            {
                TaxonomySession session = new TaxonomySession(site);
                if (session.TermStores.Count != 0)
                {
                    var termStore = session.TermStores[sTermStore];
                    var group = GetByName(termStore.Groups, sTermGroup);
                    var termSet = group.TermSets[sTermSet];
                    TaxonomyField field = site.RootWeb.Fields[fieldId] as TaxonomyField;
                    // Set Manage Metadata's Text field to Note field
                    field.TextField = noteFieldId;
                    // Connect to MMS
                    field.SspId = termSet.TermStore.Id;
                    field.TermSetId = termSet.Id;
                    field.TargetTemplate = string.Empty;
                    field.AnchorId = Guid.Empty;
                    field.Update();
                }
            }
        }
        private Group GetByName(GroupCollection groupCollection, string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Not a valid group name", "name");
            }
            foreach (var group in groupCollection)
            {
                if (group.Name == name)
                {
                    return group;
                }
            }
            throw new ArgumentOutOfRangeException("name", name, "Could not find the group");
        }
 
 
<FieldRef ID="{f3b0adf9-c1a2-4b02-920d-943fba4b3611}" DisplayName="分類法全部擷取欄" Hidden="TRUE" />
 
 
參考網站
Create Managed Metadata column (TaxonomyFieldType) in Visual Studio | Tjen Sharepoint Blog
https://tjendarta.wordpress.com/2013/07/19/create-managed-metadata-column-taxonomyfieldtype-in-visual-studio/